<?php

// Import the app/vendor/xmlrpc.php library
//App::import('Vendor', 'IXR_Library');
App::import('Vendor', 'xmlrpc');

class XmlRpcController extends AppController {

	// The XML-RPC server object
	var $server = null;
	var $uses = array();

	function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allowedActions = array('index');
	}

	function index() {
		// Disable debug information
		// Required to generate valid XML output
		Configure::write('debug', 0);

		// Avoids render() call
		$this->autoRender = false;

		// XML-RPC callbacks settings
		// Use this parameter to map XML-RPC methods to your protected or private controller methods
		$callbacks = array(
			'test.connect' => array(&$this, '_test'),
			'calendar.matchreport' => array(&$this, '_matchReport'),
		);

		// Handle XML-RPC request
		$this->server = new IXR_Server($callbacks);
	}

	// Protected Method
	function _test($username, $password) {
		if($this->_login($username, $password))
		{
			return "Login: Ok!";
		} else
		{
			return "Login: failed!";
		}

	}

	// Protected Method
	function _matchReport($username, $password, $file) {
		if($this->_login($username, $password))
		{
			$msg = "Login: Ok!";

			$fp = fopen("/tmp/last_report.sqlite", "w");
			if($fp)
			{
				if(fwrite($fp, $file) == FALSE)
					$msg .= "\nError: unable to save the temp file";
				else
					$msg .= "\nInfo: MatchReport saved!";

			} else
				$msg .= "\nError: unable to open the temp file!";

			return $msg;
		} else
		{
			return "Login: failed!";
		}
	}

	// Protected Method
	function _login($username, $password) {
		$data = array('User' => array(
			'password' => $this->Auth->password($password),
			'username' => $username));

		return $this->Auth->login($data);
	}

}

?>